package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import service.ArticleService;

@SuppressWarnings("serial")
public class ArticleServlet extends HttpServlet
{
	/**
	 * get与post的参数名
	 * 一般在getParameter方法中使用
	 * */
	public enum ArgName
	{
		GET_ARTICLE_INFORMATION_BY_COUNT("getArticleInfromationByCount"),
		GET_ARTICLE_INFORMATION_BY_ID("getArticleInformationByID"),
		GET_ARTICLE_BY_ID("getArticleByID");
		
		String name;
		
		private ArgName(String name)
		{
			this.name = name;
		}
		
		public String getName()
		{
			return this.name;
		}
	}
	
	public static ArticleService service_article = new ArticleService();
	
	
	public ArticleServlet()
	{
		
	}
	

	@Override
	public void init(ServletConfig config) throws ServletException
	{
		super.init(config);
		System.out.println(this.toString() + ".init");
		
	}

	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException
	{
		PrintWriter out = response.getWriter();
		
		String arg_get_article_information_count = request.getParameter(ArticleServlet.ArgName.GET_ARTICLE_INFORMATION_BY_COUNT.getName());
		String arg_get_article_by_id = request.getParameter(ArticleServlet.ArgName.GET_ARTICLE_BY_ID.getName());
		
		
		if (arg_get_article_information_count != null && arg_get_article_information_count != "")
		{
			System.out.println("GetArticleInformationCount: " + arg_get_article_information_count);
			int count = Integer.decode(arg_get_article_information_count);
			
			
			int count_article = ArticleServlet.service_article.getArticleCount();
			if (count > count_article)
				count = count_article;
			
			response.setContentType("application/json;charset=UTF-8");
			//response.setContentType("text/xml;charset=UTF-8");
			
			ObjectMapper mapper = new ObjectMapper();
			ArrayNode root = mapper.createArrayNode();
			//StringBuilder article_xml = new StringBuilder("<articlees>");
			
			for(int i = count; i > 0; i--)
			{
				ObjectNode node = root.addObject();
				ObjectNode temp = (ObjectNode) ArticleServlet.service_article.getArticle(i).getJSONObject();
				node.setAll(temp);
				//article_xml.append(Article.getXMLNodeString(Article.getarticle(i)));
			}
			//article_xml.append("</articlees>");
			
			
			out.print(root.toString());
			//out.print(mapper.writeValueAsString(root));
			//out.print(article_xml.toString());
			out.flush();
			out.close();
			
		}
		
		if (arg_get_article_by_id != null && arg_get_article_by_id != "")
		{
			System.out.println("GetarticleID: " + arg_get_article_by_id);
			int id = Integer.decode(arg_get_article_by_id);
			
			response.setContentType("application/json;charset=UTF-8");
			//response.setContentType("text/xml;charset=UTF-8");
			
			
			out.print(ArticleServlet.service_article.getArticle(id).getJSONObjectString());
			//out.print(Article.getXMLNodeString(Article.getarticle(id)));
			out.flush();
			out.close();
		}
	}
	
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		System.out.println(this.toString() + ".doPost");
		this.doGet(request, response);
	}
	
	
	
}





